package com.senac.faces;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.model.SelectItem;

import com.senac.model.Professor;
import com.senac.model.Titulacao;
import com.senac.service.ProfessorServiceLocal;

@ManagedBean(name = "professorMBean")
@RequestScoped
public class ProfessorMBean {

	@EJB
	private ProfessorServiceLocal service;

	private Professor professor = new Professor();
	private List<Professor> list;
	private Integer professorId;
	private String descricaoCombo;

	public String doSave() {
		try {
			service.save(professor);
		} catch(Exception e) {
			e.printStackTrace();
		}
		return "professores";
	}

	public List<Professor> getList() {
		if (list == null) {
			refreshList();
		}
		return list;
	}

	public void setList(List<Professor> list) {
		this.list = list;
	}

	public String editar() {
		professor = service.getById(professorId);
		return "professor";
	}

	public String remover() {
		service.delete(professorId);
		refreshList();
		return "professores";
	}

	public String salvar() {
		try {
			service.save(professor);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "professores";
	}

	private void refreshList() {
		list = service.get();
	}
	
	public List<SelectItem> getComboTitulacao() {
		List<SelectItem> list = new ArrayList<>(Titulacao.values().length);
		for(Titulacao titulacao : Titulacao.values()) {
			SelectItem select = new SelectItem();
			select.setValue(titulacao);
			select.setLabel(titulacao.getDescricao());
			select.setDescription(titulacao.getDescricao());
			list.add(select);
		}
		return list;
	}

	public ProfessorServiceLocal getService() {
		return service;
	}

	public void setService(ProfessorServiceLocal service) {
		this.service = service;
	}

	public Professor getProfessor() {
		return professor;
	}

	public void setProfessor(Professor professor) {
		this.professor = professor;
	}

	public Integer getProfessorId() {
		return professorId;
	}

	public void setProfessorId(Integer professorId) {
		this.professorId = professorId;
	}

	public String getDescricaoCombo() {
		return descricaoCombo;
	}

	public void setDescricaoCombo(String descricaoCombo) {
		this.descricaoCombo = descricaoCombo;
	}

}
